<?php


namespace app\common\business;


use think\Exception;

class UserAddress
{
    public $model = null;

    public function __construct() {
        //初始化模型对象
        $this->model = new \app\common\model\mysql\UserAddress();
    }

    /**
     * 获取列表
     * @param $userId
     * @return array
     */
    public function getList($userId) {
        $where = [
            ['user_id', '=', $userId]
        ];
        $order = [
            'default' => 'desc',
            'id' => 'desc'
        ];
        try {
            $res = $this->model->alias('a')
                ->field('a.*, p.name province, c.name city, d.name district')
                ->join('ProvinceCityDistrict p', 'p.id = a.province_id', 'left')
                ->join('ProvinceCityDistrict c', 'c.id = a.city_id', 'left')
                ->join('ProvinceCityDistrict d', 'd.id = a.district_id', 'left')
                ->where($where)
                ->order($order)
                ->select();
        } catch (\Exception $e) {
            return [];
        }
        return $res;
    }

    /**
     * 添加
     * @param $data
     * @return bool
     * @throws Exception
     */
    public function add($data) {
        $count = $this->getCount($data['user_id']);
        if ($count == 0) {
            $data['default'] = 1;
        } elseif ($count >= 4) {
            throw new Exception('最多只能添加4个收货地址', config('status.error'));
        }
        //添加
        try {
            $res = $this->model->save($data);
        } catch (\Exception $e) {
            return false;
        }
        return $res;
    }

    /**
     * 修改
     * @param $data
     * @return bool
     * @throws Exception
     */
    public function update($userId, $data) {
        $where = [
            ['user_id', '=', $userId],
            ['id', '=', $data['id']]
        ];
        try {
            $res = $this->model->where($where )->save($data);
        } catch (\Exception $e) {
            return false;
        }
        return $res;
    }

    /**
     * 获取地址个数
     * @param $userId
     * @return int
     */
    public function getCount($userId) {
        return $this->model->where('user_id', $userId)->count();
    }

    public function delete($userId, $id) {
        $address = $this->model->find($id);
        //删除
        $where = [
            ['id', '=',  $id],
            ['user_id', '=',  $userId],
        ];
        $res = $this->model->where($where)->delete();
        if ($res) {
            if ($address['default'] == 1) {
                $data = $this->model->where('user_id', $userId)->order('id', 'desc')->select();
                if ($data) {
                    $data = $data->toArray();
                    $this->model->where('id', $data[0]['id'])->save(['default'=> 1]);
                }

            }
            return true;
        } else {
            return false;
        }
    }

    /**
     * 默认收获地址
     * @param $userId
     * @param $id
     * @return bool
     */
    public function setDefault($userId, $id) {
        $where = [
            ['user_id', '=', $userId],
            ['id', '=', $id]
        ];
        try {
            $this->model->where('user_id', $userId)->save(['default' => 0]);
            $res = $this->model->where($where)->save(['default'=> 1]);
        } catch (\Exception $e) {
            return false;
        }
        return $res;
    }
}